// echo 客户端

#include "sock.h"
#include "rio.h"
#include "error.h"

#include <stdio.h>
#include <string.h>

#include <unistd.h>

int
main(int argc, char **argv)
{
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
    }

    sock_init();

    const char *host = argv[1];
    const char *port = argv[2];

    int clientfd = sock_open_clientfd(host, port);

    RIOT rio;
    rio_readinitb(&rio, clientfd);

    char buf[10240];
    while (fgets(buf, sizeof(buf), stdin) != NULL) {
        rio_writen(clientfd, buf, strlen(buf));
        rio_readlineb(&rio, buf, sizeof(buf));
        fputs(buf, stdout);
    }

    if (close(clientfd) != 0)
        unix_error("close fail");
    sock_cleanup();

    return 0;
}

